<script setup>
import { ref } from 'vue'
import { Promotion, User, Lock } from '@element-plus/icons-vue'
import { loginApi } from '@/api/login'

// 导入路由
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

// 创建路由实例
const router = useRouter()
let loginForm = ref({username:'', password:''})
// 登录
const login = async() => {
  const res = await loginApi(loginForm.value)
  if(res.code === 1) {
    // 对象转换为json字符串
    const userStr = JSON.stringify(res.data)
    // 保存到本地浏览器
    localStorage.setItem('loginUser', userStr)
    // 登录成功
     ElMessage.success('登录成功')
    // 跳转到首页
    router.push('/')
  } else {
    // 登录失败
    ElMessage.error(res.msg)
  }

}
  //取消
  const clear = () => {
    loginForm.value = {
      username: '',
      password: ''
    }
  }
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <el-icon class="logo-icon"><Promotion /></el-icon>
        <h1 class="title">Tlias智能学习辅助系统</h1>
      </div>
      
      <el-form label-width="80px" class="login-form">
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            size="large"
            clearable
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input 
            type="password" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            size="large"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item class="button-group">
          <el-button 
            type="primary" 
            @click="login()" 
            size="large"
            class="login-button"
          >
            登录
          </el-button>
          <el-button 
            type="info" 
            @click="clear()"
            size="large"
            class="reset-button"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: linear-gradient(120deg, #1e3c72, #2a5298);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 450px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 40px 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-icon {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 15px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.title {
  color: #303133;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  letter-spacing: 1px;
}

.login-form {
  padding: 0 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
}

:deep(.el-input__wrapper) {
  border-radius: 4px;
}

.button-group {
  margin-top: 30px;
  margin-bottom: 0;
  display: flex;
  justify-content: center;
  gap: 20px;
}

.login-button,
.reset-button {
  flex: 1;
  max-width: 120px;
  margin-bottom: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-box {
    padding: 30px 20px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .login-form {
    padding: 0 10px;
  }
  
  :deep(.el-form-item__label) {
    font-size: 14px;
  }
  
  .button-group {
    gap: 15px;
  }
  
  .login-button,
  .reset-button {
    max-width: 100px;
  }
}
</style>